package com.hfut.wxy.controller;

import com.hfut.wxy.common.exception.WxyException;
import com.hfut.wxy.common.response.Response;
import com.hfut.wxy.common.response.WxyPage;
import com.hfut.wxy.entity.mysql.WxyAppraise;
import com.hfut.wxy.entity.mysql.WxyOrder;
import com.hfut.wxy.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static com.hfut.wxy.common.response.Response.success;

@Api(tags = "订单处理")
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    IOrderService orderService;

    @ApiOperation("1 发布订单/更新订单")
    @RequestMapping(value = "/release_update",method = {RequestMethod.POST,RequestMethod.PUT})
    public Response releaseOrder(@RequestBody WxyOrder wxyOrder){
        return success(orderService.releaseOrder(wxyOrder));
    }
    @ApiOperation("1.1 查询所有的快递公司")
    @GetMapping("/companies")
    public Response company(){
        return success(orderService.companies());
    }

    @ApiOperation("2 接单")
    @GetMapping("/receive/{userId}")
    public Response receiveOrder(@PathVariable Integer userId,Integer orderId){
        return success(orderService.receiveOrder(userId,orderId));
    }

    @ApiOperation("3 接单广场")
    @GetMapping("/square")
    public Response getOrders(WxyPage page,Integer pickUpAddressId,Float commission,Integer volume){
        return success(orderService.getOrders(page,pickUpAddressId,commission,volume));
    }

    @ApiOperation("4 我的发单")
    @GetMapping("/release/{userId}")
    public Response getOrdersForOne(WxyPage page, @PathVariable Integer userId,
                                    @RequestParam(name = "states",required = false) List<Integer> states){
        return success(orderService.getOrdersForOne(page,userId,states));
    }

    @ApiOperation("5 删除订单")
    @DeleteMapping("/remove/{orderId}")
    public Response removeOrder(@PathVariable Integer orderId){
        orderService.removeOrder(orderId);
        return success;
    }

    @ApiOperation("6 确认收货")
    @PostMapping("/confirm/{orderId}")
    public Response confirmOrder(@PathVariable Integer orderId){
        return success(orderService.confirmOrder(orderId));
    }

    @ApiOperation("7 处理撤销请求")
    @PostMapping("/dispose/{orderId}")
    public Response confirmCancel(@PathVariable Integer orderId,Integer state){
        if (state!=6 && state!= 1 && state!=0){
            //如果接单人发起了撤销，发单人取消撤销传1，发单人确认撤销传6订单结束，发单人确认撤销并重发传0
            throw WxyException.of("只能继续执行或者取消订单或重发订单");
        }
        return success(orderService.confirmCancel(orderId,state));
    }

    @ApiOperation("8 我的接单")
    @GetMapping("/execute/{userId}")
    public Response myExecuteOrder(WxyPage page,@PathVariable Integer userId,
                                   @RequestParam(name = "states",required = false) List<Integer> states){
        return success(orderService.myExecuteOrder(page,userId,states));
    }

    @ApiOperation("9 完成订单")
    @PostMapping("/finish/{orderId}")
    public Response finishOrder(@PathVariable Integer orderId){
        return success(orderService.finishOrder(orderId));
    }

    @ApiOperation("10 撤销订单")
    @PostMapping("/cancel/{orderId}")
    public Response cancelOrder(@PathVariable Integer orderId,String description,Integer userId){
        return success(orderService.cancelOrder(orderId,description,userId));
    }

    @ApiOperation("11 申请仲裁")
    @PostMapping("/arbitration/{orderId}")
    // TODO: 2020/4/28 处理仲裁
    public Response arbitration(@PathVariable Integer orderId,String description){
        return success;
    }

    @ApiOperation("12 重发订单")
    @PostMapping("/re_release/{orderId}")
    public Response reRelease(@PathVariable Integer orderId){
        return success(orderService.reRelease(orderId));
    }


    @ApiOperation("13 订单评价")
    @PostMapping("/appraise")
    public Response appraise(@RequestBody WxyAppraise appraise){
        return success(orderService.appraise(appraise));
    }


}
